/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.dao.util;

import br.com.fatecpg.songs.dao.GenericDAO;
import br.com.fatecpg.songs.entity.Album;
import br.com.fatecpg.songs.entity.Artist;
import br.com.fatecpg.songs.entity.City;
import br.com.fatecpg.songs.entity.Genre;
import br.com.fatecpg.songs.entity.Musician;
import br.com.fatecpg.songs.entity.Persistent;
import br.com.fatecpg.songs.entity.Recorder;
import br.com.fatecpg.songs.entity.Song;
import br.com.fatecpg.songs.entity.User;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Marcos
 */
public class DAOFactory {
    private static Map<Class<? extends Persistent>, Class<? extends GenericDAO>> daos;
    private static Log log = LogFactory.getLog(DAOFactory.class);
    
    public static GenericDAO getDAO(Class<? extends Persistent> key) throws DAOException{
        if(daos == null){
            daos = getDaos();
        }
        try {
            return daos.get(key).newInstance();
        } catch (InstantiationException ex) {
            log.error("Erro ao invocar construtor do DAO da entitidade: " + key.getSimpleName(), ex);
            throw new DAOException(ex.getMessage());
        } catch (IllegalAccessException ex) {
            log.error("Construtor do DAO da entidade não está acessível: " + key.getSimpleName(), ex);
            throw new DAOException(ex.getMessage());
        }
        
    }
    
    public static Map<Class<? extends Persistent>, Class<? extends GenericDAO>> getDaos(){
        daos = new HashMap<Class<? extends Persistent>, Class<? extends GenericDAO>>();
        daos.put(Album.class, br.com.fatecpg.songs.dao.hibernate.AlbumDAO.class);
        daos.put(Artist.class, br.com.fatecpg.songs.dao.hibernate.ArtistDAO.class);
        daos.put(City.class, br.com.fatecpg.songs.dao.hibernate.CityDAO.class);
        daos.put(Genre.class, br.com.fatecpg.songs.dao.hibernate.GenreDAO.class);
        daos.put(Musician.class, br.com.fatecpg.songs.dao.hibernate.MusicianDAO.class);
        daos.put(Recorder.class, br.com.fatecpg.songs.dao.hibernate.RecorderDAO.class);
        daos.put(Song.class, br.com.fatecpg.songs.dao.hibernate.SongDAO.class);
        daos.put(User.class, br.com.fatecpg.songs.dao.hibernate.UserDAO.class);
        return daos;
    }
}
